<?php
/**
 * Handles configuration settings with data from the user_config admin panel
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;
use application\utilities\Utility;
use application\models\DBObject;
class UserConfig extends DBObject {

	/** 
	 * Allow users to sign up
	 *
	 * @var bool
	 */
	protected $allowSignup = TRUE;
	
	/**
	 * All signups require admin approval
	 *
	 * @var bool
	 */
	protected $adminApproval = FALSE;
	
	/**
	 * Process used to verify users when they sign up
	 *
	 * @var string
	 */
	protected $vettingProcess = 'random_password_email';
	
	/**
	 * Where to redirect user if they login with a redirect parameter
	 *
	 * @var string
	 */
	protected $postLoginRedirect = 'dashboard';
	
	/**
	 * Custom location for login redirect if postLoginRedirect is set to 'custom'
	 *
	 * @var string
	 */
	protected $postLoginCustom;
	
	/**
	 * How a user is identifed for displayName()
	 *
	 * @var string
	 */
	protected $identification = 'email';
	
	/**
	 * Custom user identification using field display names
	 *
	 * @var string
	 */
	protected $identificationCustom;

	
	/**
	 * Initialize the configuration with existing option values
	 */
	public function __construct() {
		$this->allowSignup = $this->getOption('allow_signup');
		$this->adminApproval = $this->getOption('admin_approval');
		$this->vettingProcess = $this->getOption('vetting_process');
		$this->postLoginRedirect = $this->getOption('post_login_redirect');
		$this->postLoginCustom = $this->getOption('post_login_custom');
		$this->identification = $this->getOption('identification');
		$this->identificationCustom = $this->getOption('identification_custom');
	}
	
	/**
	 * Get a user option
	 *
	 * @param string $option
	 * @return string
	 */
	protected function getOption($option) {
		return getOption('user_'.$option);
	}
	
	/**
	 * Set a user option
	 *
	 * @param string $option
	 * @param string $value
	 */
	protected function setOption($option, $value) {
		setOption('user_'.$option, $value);
	}
	
	/**
	 * Update all options with the properties on this object
	 */
	protected function updateSystemProperties() {
		$this->setOption('allow_signup', $this->allowSignup);
		$this->setOption('admin_approval', $this->adminApproval);
		$this->setOption('vetting_process', $this->vettingProcess);
		$this->setOption('post_login_redirect', $this->postLoginRedirect);
		$this->setOption('post_login_custom', $this->postLoginCustom);
		$this->setOption('identification', $this->identification);
		$this->setOption('identification_custom', $this->identificationCustom);
	}
	
	/**
	 * Get values from a form and then update options
	 */
	public function save() {
		global $concerto;
		$this->allowSignup = $_REQUEST['allow_signup'];
		$this->adminApproval = $_REQUEST['admin_approval'];
		$this->vettingProcess = $_REQUEST['vetting_process'];
		$this->postLoginRedirect = $_REQUEST['post_login_redirect'];
		$this->postLoginCustom = $_REQUEST['post_login_custom'];
		$this->identification = $_REQUEST['identification'];
		$this->identificationCustom = $_REQUEST['identification_custom'];
		$this->updateSystemProperties();
	}
}

?>